/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Collections;
using System.IO;
using System.Reflection;

using _videoConference.BAL;

namespace _videoConference
{
    public partial class AddModuleControl : System.Windows.Controls.UserControl, IDisposable
    {
        IAddPage ap = new IAddPage();
        DataSet ds = new DataSet();
        ComboBoxItem lbi;
        System.Reflection.Assembly a;
        System.Reflection.Assembly b = System.Reflection.Assembly.GetAssembly(typeof(AddModuleControl));
        ArrayList al = new ArrayList();
        private bool disposed = false;
        
        public AddModuleControl()
        {
            InitializeComponent();
            
            DataSet dsmodule = ap.getdllinfo();

            if (dsmodule.Tables.Count > 0 && dsmodule.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < dsmodule.Tables[0].Rows.Count; i++)
                {
                    ComboBoxItem cmbdllname = new ComboBoxItem();
                    cmbdllname.Content = dsmodule.Tables[0].Rows[i][1].ToString();
                    cmbdllname.Tag = dsmodule.Tables[0].Rows[i][0].ToString();
                    cmbdllname.Background = Brushes.Black;
                    lbDllName.Items.Add(cmbdllname);
                }
            }
        }

        public void update(object o, EventArgs e)
        {
            if (lbDllName.SelectedItem == null || lbDllName.SelectedItem.ToString() == "")
            {
                MessageBox.Show("Choose the dllname");
                return;
            }
            else
            {
                if (cmbClassName.Text == "" || cmbClassName.Text == null)
                {
                    MessageBox.Show("Choose the classname");
                    return;
                }
                else
                {
                    if (txtModuleName.Text == "" || txtModuleName.Text == null)
                    {
                        MessageBox.Show("Enter the module name");
                        return;
                    }
                    else
                    {
                        lbi = (ComboBoxItem)lbDllName.SelectedItem;
                        TextRange tr = new TextRange(txtModuleDesc.Document.ContentStart, txtModuleDesc.Document.ContentEnd);

                        int result=ap.checkmodule(txtModuleName.Text);
                        if (result == 0)
                        {
                            if (UserInfo.moduleid!= "")
                            {
                                bool b=ap.updmodule(UserInfo.moduleid,txtModuleName.Text,tr.Text,txtModuleVersion.Text);
                                UserInfo.moduleid = "";
                            }
                            else
                            {
                                ap.dbmodule(lbi.Content.ToString(), cmbClassName.Text, txtModuleName.Text, tr.Text, txtModuleVersion.Text, lbi.Tag.ToString());                                
                            }
                            ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
                            ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = true;
                            ((InkCanvas)this.Parent).Children.Remove(this);
                        }
                        else
                        {
                            MessageBox.Show("Module name already exist.Enter another");
                            txtModuleName.Text = "";
                        }                       
                    }
                }
            }
        }

        public void ShowDirectory(DirectoryInfo dir)
        {
            foreach (FileInfo file in dir.GetFiles())
            {
                int hj = al.Add(file.FullName);
            }
            foreach (DirectoryInfo subDir in dir.GetDirectories())
            {
                ShowDirectory(subDir);
            }
        }

        public void dllchanged(object o, SelectionChangedEventArgs e)
        {
            cmbClassName.Items.Clear();
            if (lbDllName.SelectedItem != null)
            {
                lbi = (ComboBoxItem)lbDllName.SelectedItem;
                DataSet dsclass = ap.getclassdll(lbi.Tag.ToString());

                if (dsclass.Tables.Count > 0 && dsclass.Tables[0].Rows.Count > 0)
                {
                    for (int j = 0; j < dsclass.Tables[0].Rows.Count; j++)
                    {
                        ComboBoxItem cbi = new ComboBoxItem();
                        cbi.Content = dsclass.Tables[0].Rows[j][3].ToString();
                        cbi.Tag = dsclass.Tables[0].Rows[j][0].ToString();
                        cbi.Foreground = Brushes.White;
                        cbi.Background = Brushes.Black;
                        cmbClassName.Items.Add(cbi);
                    }
                }
            }
            cmbClassName.SelectionChanged += new SelectionChangedEventHandler(cmbClassName_SelectionChanged);
        }

        void cmbClassName_SelectionChanged(object sender, SelectionChangedEventArgs e)            
        {            
        }

        public void cancel(object o, EventArgs e)
        {
            txtModuleName.Text = "";
            TextRange tr = new TextRange(txtModuleDesc.Document.ContentStart, txtModuleDesc.Document.ContentEnd);
            tr.Text = "";
            txtModuleVersion.Text = "";
            lbDllName.Items.Clear();
            ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
            ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = true;
            ((InkCanvas)this.Parent).Children.Remove(this);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {

                }
                disposed = true;

            }
        }

        ~AddModuleControl()
        {
            Dispose(false);
        }
    }
}